/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "ConstantBuffer.h"
#include "Hogshead.Core.h"
#include <Windows.h>
#include <D3DX11.h>
#include "../Managers/DX11Manager.h"

namespace Hogshead
{
	namespace DX11Rendering
	{
		ConstantBuffer::ConstantBuffer(void) : _shell(16)
		{
			_register_num = -1;
			_start_address = NULL;
			_size = 0;
			_shader_bind_flags = (ShaderBindFlags)0;
			_up_to_date = false;
			_buffer = NULL;
		}


		ConstantBuffer::~ConstantBuffer(void)
		{
			if(_start_address)
				Factory::releaseMemoryBlock( _start_address );

			if(_buffer)
				_buffer->Release();
		}

		ConstantBuffer::ConstantBuffer(const ConstantBuffer& in_other)
		{
			_register_num = in_other._register_num;
			if( in_other._start_address )
			{
				_start_address = (byte*) Factory::acquireMemoryBlock( in_other._size );
				ZeroMemory(_start_address, in_other.size());
				_size = in_other._size;

				memcpy(_start_address, in_other._start_address, _size);
			}
			else
			{
				_start_address = NULL;
				_size = 0;
			}
			
			_shader_bind_flags = in_other._shader_bind_flags;

			//need to handle HashTable
			_shell = in_other._shell;

			//need to get a Buffer object
			if( _size == 0 )
				error("bad buffer");

			if( FAILED ( DX11Manager::createConstantBuffer( _size, &_buffer) ) )
				error("couldn't create cbuffer");

			_up_to_date = false;
		}

		ConstantBuffer& ConstantBuffer::operator=(const ConstantBuffer& in_other)
		{
			_register_num = in_other._register_num;
			if( in_other._start_address )
			{
				if( _start_address )
					Factory::releaseMemoryBlock( _start_address );

				_start_address = (byte*) Factory::acquireMemoryBlock( in_other._size );
				ZeroMemory(_start_address, in_other.size());
				_size = in_other._size;

				memcpy(_start_address, in_other._start_address, _size);
			}
			else
			{
				_start_address = NULL;
				_size = 0;
			}
			
			_shader_bind_flags = in_other._shader_bind_flags;

			//need to handle HashTable
			_shell = in_other._shell;

			//need to get a buffer object after possibly releasing old object
			if( _buffer )
				_buffer->Release();

			/*if( _size == 0 )
				error("bad buffer");*/
			if( _size > 0 )
			{
				if( FAILED ( DX11Manager::createConstantBuffer( _size, &_buffer) ) )
					error("couldn't create cbuffer");
			}
	

			_up_to_date = false;

			return *this;
		}

		void ConstantBuffer::build( UINT in_register, ShaderBindFlags in_flags )
		{
			//do error checking for byte size multiple of 16.
			if( _size % 16 != 0 )
				error("A ConstantBuffer's size is not a multiple of 16 bytes.");

			//copy in information
			_shader_bind_flags = in_flags;
			_register_num = in_register;
			
			//get memory block
			_start_address = (byte*) Factory::acquireMemoryBlock( _size );

			//now actually create the buffer and get an interface pointer
			if( FAILED( DX11Manager::createConstantBuffer( _size, &_buffer ) ) )
				error("Couldn't create cbuffer.");

			_up_to_date = false;
		}

		void ConstantBuffer::addPrimitive( const String& in_name, UINT in_size_of_primitive )
		{
			_shell.add( in_name, _size );
			_size += in_size_of_primitive;
		}

		void ConstantBuffer::clear()
		{
			if( _start_address )
			{
				Factory::releaseMemoryBlock( _start_address );
				_start_address = NULL;
			}

			if( _buffer )
			{
				_buffer->Release();
				_buffer = NULL;
			}

			_register_num = -1;
			_size = 0;

			_shader_bind_flags = (ShaderBindFlags)0;
			_up_to_date = false;

			_shell.clear();
		}

		UINT ConstantBuffer::registerNumber() const
		{
			return _register_num;
		}

		UINT ConstantBuffer::size() const
		{
			return _size;
		}

		ShaderBindFlags ConstantBuffer::shaderBindFlags() const
		{
			return _shader_bind_flags;
		}

		ID3D11Buffer* const* ConstantBuffer::buffer()
		{
			//return it if it hasn't been changed
			if(_up_to_date)
				return &_buffer;

			//if it has been changed, update the buffer
			DX11Manager::updateConstantBuffer(_buffer, _start_address);
			_up_to_date = true;

			return &_buffer;
		}

		bool ConstantBuffer::operator==(const ConstantBuffer& right) const
		{
			error("Equivalence operator for ConstantBuffer is not implemented");
			return false;
		}
	}
}